package com.example.demo.contoller;

import com.example.demo.dao.VideoDao;
import com.example.demo.model.JsonData;
import com.example.demo.model.VideoDO;
import com.example.demo.utils.JsonUtil;
import com.example.demo.vo.CartItemVO;
import com.example.demo.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 购物车
 */
@RestController
@RequestMapping("/api/cart")
public class CartController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ApplicationArguments applicationArguments;

    /**
     * 增加购物车
     *  根据视频id videoId进行判断 购物车中存在，则增加数量，否则增改id对应的视频对象
     * @param videoId
     * @param byNum
     * @return
     */
    @RequestMapping("/add")
    public JsonData addCart(int videoId,int byNum){
        String videoIdStr = Integer.toString(videoId);
        BoundHashOperations<String, Object, Object> cart = getCart(key());
        Object video = cart.get(videoIdStr);
        String result = "";
        if(video != null){
            result = (String) video;
            CartItemVO cartItemVO = JsonUtil.toObject(result, CartItemVO.class);
            cartItemVO.setByNum(cartItemVO.getByNum() + byNum);
            cart.put(videoIdStr,JsonUtil.toJsonString(cartItemVO));
        }else{
            VideoDO detailById = VideoDao.findDetailById(videoId);
            CartItemVO cartItemVO = new CartItemVO();
            cartItemVO.setPrice((int)detailById.getPrice());
            cartItemVO.setByNum(byNum);
            cartItemVO.setProductId(detailById.getId());
            cartItemVO.setProductTitle(detailById.getTitle());
            cartItemVO.setProductImg(detailById.getImg());
            cart.put(videoIdStr, JsonUtil.toJsonString(cartItemVO));
        }
        return JsonData.buildSuccess();
    }

    /**
     * 查看购物车
     * @return
     */
    @RequestMapping("/show")
    public JsonData getMyCart(){
        BoundHashOperations<String, Object, Object> cart = getCart(key());
        List<Object> values = cart.values();
        List<CartItemVO> cartItemVOS = new ArrayList<>();
        for(Object value: values){
            cartItemVOS.add(JsonUtil.toObject((String) value,CartItemVO.class));
        }
        CartVO cartVO = new CartVO();
        cartVO.setCartItems(cartItemVOS);
        return JsonData.buildSuccess(cartVO);
    }

    /**
     * 清空购物车
     * @return
     */
    @RequestMapping("/del")
    public JsonData delCart(){
        redisTemplate.delete(key());
        return JsonData.buildSuccess();
    }

    /**
     * 根据key获取对应的购物车HashMap
     * @param key
     * @return
     */
    public BoundHashOperations<String,Object,Object> getCart(String key){
        return redisTemplate.boundHashOps(key);
    }
    /**
     * 获取用户id 进一步获取redis中hashMap对应的key
     * @return
     */
    public String key(){
        //从拦截器中获取 生产
        String userId = "86";
        return String.format("video:user:%s",userId).toString();
    }



}
